1 00:00:00,400 --> 00:00:01,720 Hey welcome back. 2 00:00:01,720 --> 00:00:07,180 In this lecture we're going to take a look at another global function in Lua called Loadstring. 3 00:00:07,180 --> 00:00:12,280 This is a special function reserved for server scripts only and it can't be used by the client. 4 00:00:12,280 --> 00:00:17,620 The purpose of the Loadstring is to take a string input and convert that string into a function that 5 00:00:17,620 --> 00:00:18,580 can be executed. 6 00:00:18,580 --> 00:00:23,830 So if you wanted to create a custom command line for players of a specific rank in your game, and you 7 00:00:23,830 --> 00:00:29,050 want them to be able to input code and execute it on the server, you would use remotes and the Loadstring 8 00:00:29,050 --> 00:00:29,680 function. 9 00:00:29,680 --> 00:00:33,880 However, you have to be extremely careful with the Loadstring function. 10 00:00:33,880 --> 00:00:39,070 If you do not ensure your remote functions are secure, exporters could execute server side code and 11 00:00:39,070 --> 00:00:40,180 ruin your game. 12 00:00:40,180 --> 00:00:45,340 You'd also want to make sure that anyone who has access to the Loadstring function can be trusted. 13 00:00:45,340 --> 00:00:51,250 Otherwise, an individual could modify data stores in your game and just ruin a whole bunch of stuff. 14 00:00:51,250 --> 00:00:55,300 So while the Loadstring function is neat, it can be very dangerous. 15 00:00:55,720 --> 00:01:01,390 So to use the Loadstring function, you first must enable a property in the server script service called 16 00:01:01,390 --> 00:01:02,860 Loadstring enabled. 17 00:01:03,160 --> 00:01:05,320 When you hit the check box, a warning is going to pop up. 18 00:01:05,320 --> 00:01:09,910 It's telling you activating the Loadstring enabled property might make your game vulnerable to exploits. 19 00:01:09,910 --> 00:01:11,560 Are you sure you want to enable this? 20 00:01:11,560 --> 00:01:15,340 We're going to hit yes, and it's telling you that it can make your game vulnerable. 21 00:01:15,340 --> 00:01:21,070 If you set up the loadstring in a way where it can be exploited by somebody on the client. 22 00:01:21,700 --> 00:01:25,480 But I'm just going to show you an example of how we can use the Loadstring function. 23 00:01:26,200 --> 00:01:31,000 So I'm going to create a string here and I'm going to call it my string code. 24 00:01:33,290 --> 00:01:37,040 And inside of here we can act like we're just scripting a regular script. 25 00:01:37,040 --> 00:01:41,390 So I could put like a print statement in here and say something like, print hello, world. 26 00:01:43,300 --> 00:01:50,200 And then I could also do something like create variables, and then I could use if statements to check 27 00:01:50,200 --> 00:01:53,980 those variables and do something based on that, uh, condition. 28 00:01:53,980 --> 00:02:00,610 And just you can do anything like you would with a regular script, write any code that you use would 29 00:02:00,610 --> 00:02:01,300 execute. 30 00:02:01,860 --> 00:02:05,790 Now you can take the string and you can put it into the load string function. 31 00:02:05,790 --> 00:02:07,830 So load string is global. 32 00:02:07,830 --> 00:02:10,980 And then you can pass the string which would be my string code. 33 00:02:10,980 --> 00:02:16,950 And as long as the formatting or the syntax of the code in here matches Lua, you, then you can execute 34 00:02:16,950 --> 00:02:17,340 it. 35 00:02:17,340 --> 00:02:21,300 And the load string function returns back to us a function that can be executed. 36 00:02:21,300 --> 00:02:26,850 So I can call it like some function, and then the string will get converted into code stored in this 37 00:02:26,850 --> 00:02:27,510 function. 38 00:02:27,510 --> 00:02:29,790 And that means we can go and call the function. 39 00:02:29,790 --> 00:02:33,600 So if I call some function it should print out hello world. 40 00:02:33,600 --> 00:02:34,920 And the bool was true. 41 00:02:34,920 --> 00:02:36,510 So if we hit run. 42 00:02:38,660 --> 00:02:39,230 There we go. 43 00:02:39,230 --> 00:02:40,850 You see on the server hello world. 44 00:02:40,850 --> 00:02:42,620 And the bool was true. 45 00:02:42,620 --> 00:02:46,670 So this gives us the ability to execute code from a string. 46 00:02:46,670 --> 00:02:51,710 So if you ever wanted to create your own custom command line and not use the one that's in the f nine 47 00:02:51,710 --> 00:02:53,630 console, you could totally do it this way. 48 00:02:53,630 --> 00:02:59,120 But again, you have to be very careful and make sure to secure remotes, because anybody who has the 49 00:02:59,120 --> 00:03:03,440 ability to execute server side code can mess up a lot of things in your game. 50 00:03:04,440 --> 00:03:07,860 But let's go ahead and look at another example of using the load string. 51 00:03:07,860 --> 00:03:12,120 I'll create another string, call it my string code two. 52 00:03:13,050 --> 00:03:16,230 And here something I could do like is create a variable. 53 00:03:16,230 --> 00:03:17,280 I'll just call it player. 54 00:03:17,280 --> 00:03:20,460 And we can just get a player randomly inside of the player service. 55 00:03:20,460 --> 00:03:26,190 So I'll just find first child which is a player. 56 00:03:27,310 --> 00:03:30,400 Or if there isn't one, then I could just wait for one to be added. 57 00:03:36,780 --> 00:03:38,820 And then I can print out the name of this player. 58 00:03:38,820 --> 00:03:43,830 So just like any other regular code in our game and again we can create a variable, I'll call it sum 59 00:03:43,830 --> 00:03:45,030 function two. 60 00:03:45,030 --> 00:03:51,060 And we can use the load string function and pass the string to create an executable function. 61 00:03:51,060 --> 00:03:53,040 And then we can call this function. 62 00:03:55,370 --> 00:03:58,940 And now we should get my name printed out into the console. 63 00:03:59,990 --> 00:04:00,830 There we go. 64 00:04:00,890 --> 00:04:01,730 Hello world! 65 00:04:01,730 --> 00:04:04,850 The bool is true and Udemy lul you course. 66 00:04:05,460 --> 00:04:08,730 So this was just a brief example of the load string function. 67 00:04:08,730 --> 00:04:12,960 The use case for this function is pretty limited, but if you ever wanted to use it, the option is 68 00:04:12,960 --> 00:04:13,770 there for you. 69 00:04:13,770 --> 00:04:19,170 Just make sure to be careful with your remotes and not to trust people you don't trust with the function. 70 00:04:19,620 --> 00:04:20,850 See you in the next lecture.